#include <process.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <graphics.h>
#include <fcntl.h>
#include <string.h>
#include <io.h>
#include "chinese.hpp"

FILE *handle;

int chinese::openhzk(char c)
{
	if(c=='s')
	   handle=fopen(".\\hzk16","rb");
	if(c=='k')
	   handle=fopen(".\\clib24k","rb");
	if(c=='f')
	   handle=fopen(".\\hzk16f","rb");
	if(c=='b')
	   handle=fopen(".\\clib24h","rb");
	if(c=='h')
	   handle=fopen(".\\clib40h","rb");
	if(c=='d')
	   handle=fopen(".\\clib48s","rb");
	if(c=='x')
	   handle=fopen(".\\clib16","rb");
	if(handle==NULL)
	{
	   cputs("Error on open the lib.");
	   exit(1);
	}
	return 0;
}

int chinese::close(void)
{
	fclose(handle);
	return 0;
}


int chinese::puthz16(int x,int y,int z,int color,char *p)
{
	unsigned int i,c1,c2,m=0;
	int i1,i2,i3,rec;
	long l;
	char by[32];
	while((i=*p++)!=0)
	{
	   if(i>0xal)
	     if(m==0)
	     {
		 c1=(i-0xa1)&0x07f;
		 m=1;
	     }
	     else
	     {
		 c2=(i-0xa1)&0x07f;
		 m=0;
		 rec=c1*94+c2;
		 l=rec*32L;
		 fseek(handle,l,SEEK_SET);
		 fread(by,32,1,handle);
		 for(i1=0;i1<16;i1++)
		   for(i2=0;i2<2;i2++)
		     for(i3=0;i3<8;i3++)
		       if(getbit(by[i1*2+i2],7-i3))
			 putpixel(x+i2*8+i3,y+i1,color);
			 x=x+16+z;
	       }
	  }
	  return(x);
}


int chinese::puthz16_new(int x,int y,int z,int color,char *p,int maxwidth)
{
	unsigned int i,c1,c2,m=0;
	int i1,i2,i3,rec,x1;
	long l;
	char by[32];
	x1=x;
	while((i=*p++)!=0)
	{
	   if(i>0xal)
	     if(m==0)
	     {
		c1=(i-0xa1)&0x07f;
		m=1;
	     }
	     else
	     {
		c2=(i-0xa1)&0x07f;
		m=0;
		rec=c1*94+c2;
		l=rec*32L;
		fseek(handle,l,SEEK_SET);
		fread(by,32,1,handle);
		for(i1=0;i1<16;i1++)
		  for(i2=0;i2<2;i2++)
		    for(i3=0;i3<8;i3++)
		     if(getbit(by[i1*2+i2],7-i3))
		       putpixel(x+i2*8+i3,y+i1,color);
		     x=x+16+z;
			if(x>=x1+maxwidth)
			{
			  x=x1;
			  y =y+20;
			}
	     }
	}
	return(y+16);
}

int chinese::puthz16a(int x,int y,int z,int color,char *p)
{
	unsigned int i,c1,c2,m=0;
	int i1,i2,i3,rec;
	long l;
	char by[32];
	char tt[2];
	while((i=*p++)!=0)
	{
	   if(i>161)
	    {if(m==0)
	     {
		 c1=(i-0xa1)&0x07f;
		 m=1;
	     }
	     else
	     {
		 c2=(i-0xa1)&0x07f;
		 m=0;
		 rec=c1*94+c2;
		 l=rec*32L;
		 fseek(handle,l,SEEK_SET);
		 fread(by,32,1,handle);
		 for(i1=0;i1<16;i1++)
		   for(i2=0;i2<2;i2++)
		     for(i3=0;i3<8;i3++)
		       if(getbit(by[i1*2+i2],7-i3))
			 putpixel(x+i2*8+i3,y+i1,color);
			 x=x+16+z;
	     }
	    }
	    else
	    {
	     tt[0]=i;
	     tt[1]='\0';
	     setcolor(color);
	     settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
	     settextjustify(LEFT_TEXT,RIGHT_TEXT);
	     outtextxy(x,y+4,tt);
	     x=x+8;
	    }
	  }
	  return(x);
}


int chinese::puthz24a(int x,int y,int z,int color,char *p)
{
	unsigned int i,c1,c2,f=0;
	int i1,i2,i3,rec;
	long l;
	char by[72];
	char tt[2];
	int style;

	while((i=*p++)!=0)
	{
	    if(i>161)
	     { if(f==0)
	       {
		  c1=(i-0xa1)&0x07f;
		  f=1;
	       }
	       else
	       {
		  c2=(i-0xa1)&0x07f;
		  f=0;
		  rec=c1*94+c2;
		  l=rec*72L;
		  fseek(handle,l,SEEK_SET);
		  fread(by,72,1,handle);
		  for(i1=0;i1<24;i1++)
		    for(i2=0;i2<=2;i2++)
		      for(i3=0;i3<8;i3++)
			if(getbit(by[i1*3+i2],7-i3))
			   putpixel(x+i1,y+i2*8+i3,color);
			   x=x+24+z;
	       }
	     }
	    else
	    {
	     tt[0]=i;
	     tt[1]='\0';
	     setcolor(color);

	     settextstyle(0, HORIZ_DIR, 2);

	     outtextxy(x,y+4,tt);
	     x=x+24;
	    }
	}
	    return(x);
}

int chinese::puthz24(int x,int y,int z,int color,char *p)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,rec;
	long l;
	char by[72];
	while((i=*p++)!=0)
	{
	    if(i>0xa1)
	      if(f==0)
	      {
		  c1=(i-0xa1)&0x07f;
		  f=1;
	      }

	      else
	      {
		  c2=(i-0xa1)&0x07f;
		  f=0;
		  rec=c1*94+c2;
		  l=rec*72L;
		  fseek(handle,l,SEEK_SET);
		  fread(by,72,1,handle);
		  for(i1=0;i1<24;i1++)
		    for(i2=0;i2<=2;i2++)
		      for(i3=0;i3<8;i3++)
			if(getbit(by[i1*3+i2],7-i3))
			   putpixel(x+i1,y+i2*8+i3,color);
			   x=x+24+z;
	      }
	    }
	    return(x);
}

int chinese::puthz24_x(int x,int y,int z,int color,char *p,int returnx,int xlimit)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,rec;
	long l;
	char by[72];
	while((i=*p++)!=0)
	{
	    if(i>0xa1)
	      if(f==0)
	      {
		  c1=(i-0xa1)&0x07f;
		  f=1;
	      }

	      else
	      {
		  c2=(i-0xa1)&0x07f;
		  f=0;
		  rec=c1*94+c2;
		  l=rec*72L;
		  fseek(handle,l,SEEK_SET);
		  fread(by,72,1,handle);
		  for(i1=0;i1<24;i1++)
		    for(i2=0;i2<=2;i2++)
		      for(i3=0;i3<8;i3++)
			if(getbit(by[i1*3+i2],7-i3))
			   putpixel(x+i1,y+i2*8+i3,color);
			   x=x+24+z;
			   if(x>xlimit-24)
			   {
			     x=returnx;
			     y=y+30;
			   }

	      }
	    }
	    return(x);
}

int  chinese::puthz24_new(int x,int y,int z,int color,char *p,int maxwidth,int flag)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,rec,x1;
	long l;
	char by[72];
	x1=x;
	if(flag==2) x=x+48;
	while((i=*p++)!=0)
	{
	   if(i>0xa1)
	     if(f==0)
	     {
		c1=(i-0xa1)&0x07f;
		f=1;
	     }
	     else
	     {
		c2=(i-0xa1)&0x07f;
		f=0;
		rec=c1*94+c2;
		l=rec*72L;
		fseek(handle,l,SEEK_SET);
		fread(by,72,1,handle);
		for(i1=0;i1<24;i1++)
		  for(i2=0;i2<=2;i2++)
		    for(i3=0;i3<8;i3++)
		      if(getbit(by[i1*3+i2],7-i3))
			putpixel(x+i1,y+i2*8+i3,color);
			x=x+24+z;
			if(x>=x1+maxwidth)
			{
			  x=x1;
			  y =y+30;
			}
	       }
	}
	return(y+30);
}



// **********************************************




int chinese::puthz40(int x,int y,int z,int color,char *p,int maxwidth,int flag)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,rec,x1;
	long l;
	char by[180];
	x1=x;
	if(flag==2) x=x+72;
	while((i=*p++)!=0)
	{
	    if(i>0xa1)
	      if(f==0)
	      {
		 c1=(i-0xb0)&0x07f;
		 f=1;
	      }
	      else
	      {
		 c2=(i-0xa1)&0x07f;
		 f=0;
		 rec=c1*94+c2;
		 l=rec*180L;
		 fseek(handle,l,SEEK_SET);
		 fread(by,180,1,handle);
		 for(i1=0;i1<36;i1++)
		   for(i2=0;i2<2;i2++)
		     for(i3=0;i3<8;i3++)
		       if(getbit(by[i1*2+i2],7-i3))
			 putpixel(x+i1,y+i2*8+i3,color);
			 for(i1=0;i1<36;i1++)
			    for(i2=0;i2<=2;i2++)
			      for(i3=0;i3<8;i3++)
				if(getbit(by[i1*3+i2+72],7-i3))
				   putpixel(x+i1,y+16+i2*8+i3,color);
				   x=x+36+z;
				   if(x>=x1+maxwidth)
				   {
				       x=x1;
				       y=y+45;
				   }
	      }
       }
       return(y+60);
}

// ************************************************

int chinese::puthz48(int x,int y,int z,int color,char *p,int maxwidth,int flag)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,rec,x1;
	long l;
	char by[270];
	x1=x;
	if(flag==2) x=x+96;
	while((i=*p++)!=0)
	{
	   if(i>0xa1)
	     if(f==0)
	     {
		c1=(i-0xb0)&0x07f;
		f=1;
	     }
	     else
	     {
		c2=(i-0xa1)&0x07f;
		f=0;
		rec=c1*94+c2;
		l=rec*270L;
		fseek(handle,l,SEEK_SET);
		fread(by,270,1,handle);
		for(i1=0;i1<45;i1++)
		  for(i2=0;i2<=2;i2++)
		    for(i3=0;i3<8;i3++)
		      if(getbit(by[i1*3+i2],7-i3))
			putpixel(x+i1,y+i2*8+i3,color);
		      for(i1=45;i1<90;i1++)
			for(i2=0;i2<3;i2++)
			   for(i3=0;i3<8;i3++)
			     if(getbit(by[i1*3+i2],7-i3))
				putpixel(x+i1-45,y+24+i2*8+i3,color);
				x=x+45+z;
			     if(x>=x1+maxwidth)
			     {
				x=x1;
				y =y+80;
			     }
	     }
       }
       return(y+80);
}

//******************************************************

int chinese::puthz24rat(int x,int y,int z,int color,char *p,char d,int angle)
{
	unsigned int i,c1,c2,f=0;
	int i1,i2,i3,rec;
	long l;
	char by[72];
	while((i=*p++)!=0)
	{
	    if (i>0xa1)
	    if (f==0)
	    {
		c1=(i-0xa1)&0x07f;
		f=1;
	    }
	    else
	    {
		c2=(i-0xa1)&0x07f;
		f=0;
		rec=c1*94+c2;
		l=rec*72L;
		fseek(handle,l,SEEK_SET);
		fread(by,72,1,handle);

		if (angle==0)
		for (i1=0;i1<24;i1++)
		  for (i2=0;i2<=2;i2++)
		     for (i3=0;i3<8;i3++)
		     if (getbit(by[i1*3+i2],7-i3))
			putpixel(x+i1,y+i2*8+i3,color);
		     if (angle==90)
		for (i1=0;i1<24;i1++)
		  for (i2=0;i2<=2;i2++)
		     for (i3=0;i3<8;i3++)
		     if (getbit(by[i1*3+i2],7-i3))
			putpixel(x+i2*8+i3,y-i1,color);
		     if (d=='h')
			x=x+24+z;
		     if (d=='v')
		     {
			if (angle==0)
			  y=y+24+z;
			if (angle==90)
			  y=y-24-z;
		      }
	    }
	}
	return x;
}


int chinese::puthz24ext(int x,int y,int z,int color,int m,int n,char *p)
{
	unsigned int i,c1,c2,c3,f=0;
	int i1,i2,i3,i4,i5,rec;
	long l;
	char by[72];
	while((i=*p++)!=0)
	{
	    if(i>0xa1)
	      if(f==0)
	      {
		  c1=(i-0xa1)&0x07f;
		  f=1;
	      }

	      else
	      {
		  c2=(i-0xa1)&0x07f;
		  f=0;
		  rec=c1*94+c2;
		  l=rec*72L;
		  fseek(handle,l,SEEK_SET);
		  fread(by,72,1,handle);
		  for(i1=0;i1<24*m;i1=i1+m)
		    for(i4=0;i4<m;i4++)
		     for (i2=0;i2<=2;i2++)
		      for(i3=0;i3<8;i3++)
			if(getbit(by[i1/m*3+i2],7-i3))
			  for (i5=0;i5<n;i5++)
			   putpixel(x+i1+i4,y+i2*8*n+i3*n+i5,color);
			   x=x+24*m+z;
	      }
	    }
	    return(x);
}
